class Admin::PeopleController < ApplicationController
  # GET /admin_people
  def index
    @people = Person.paginate :page => params[:page], :per_page => 15, :order => 'last_name, first_name', :conditions => 'deleted <> 1'

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @people }
    end
  end

  # GET /admin_people/1
  def show
    @person = Person.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @person }
    end
  end

  # GET /admin_people/new
  def new
    @person = Person.new
    #listy do selectow licencji
    @rally_licenses   = [["brak",0]].concat(License.find(:all, :conditions => ["kind = 'rajdowa'"], :order => "name").map {|l| [l.name, l.id]})
    @referee_licenses = [["brak",0]].concat(License.find(:all, :conditions => ["kind = 'sędziowska'"], :order => "name").map {|l| [l.name, l.id]})
    #ewentualne licencje
    @rally_license    = License.new
    @referee_license  = License.new
    #miasto
    @city = City.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @person }
    end
  end

  # GET /admin_people/1/edit
  def edit
    #listy do selectow licencji
    @rally_licenses   = [["brak",0]].concat(License.find(:all, :conditions => ["kind = 'rajdowa'"], :order => "name").map {|l| [l.name, l.id]})
    @referee_licenses = [["brak",0]].concat(License.find(:all, :conditions => ["kind = 'sędziowska'"], :order => "name").map {|l| [l.name, l.id]})
    #edytowany koles
    @person           = Person.find(params[:id])
    @rally_license    = @person.current_rally_license ? @person.current_rally_license : License.new
    @referee_license  = @person.current_referee_license ? @person.current_referee_license : License.new
    #miasto
    @city             = @person.city ? @person.city : City.new 
  end

  # POST /admin_people
  def create
    @person       = Person.new(params[:person])
    @person.city  = City.find_or_create_by_name(params[:city][:name])
 
    respond_to do |format|
      if @person.save
        flash[:notice] = 'Dodano osobę do bazy.'
        format.html { redirect_to([:admin, @person]) }
        format.xml  { render :xml => @person, :status => :created, :location => @person }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @person.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /admin_people/1
  def update
    @person = Person.find(params[:id])
    #miasto
    @city = City.find_or_create_by_name(params[:city][:name])
    @person.city = @city
    #znalezienie i dodanie licencji sedziowskiej
    begin
      @referee_license = License.find(params[:referee_license][:id])
      @people_license = PeopleLicense.find_or_create_by_person_id_and_license_id_and_year_and_is_valid(@person.id,@referee_license.id,DateTime.now.year,1)
    rescue
    end
    #lub rajdowej
    begin
      @rally_license = License.find(params[:rally_license][:id])
      @people_license = PeopleLicense.find_or_create_by_person_id_and_license_id_and_year_and_is_valid(@person.id,@rally_license.id,DateTime.now.year,1)
    rescue
    end

    respond_to do |format|
      if @person.update_attributes(params[:person])
        flash[:notice] = 'Zapisano zmiany.'
        format.html { redirect_to([:admin, @person])}
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @person.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /admin_people/1
  def destroy
    @person = Person.find(params[:id])
    @person.deleted = 1
    @person.save

    respond_to do |format|
      format.html { redirect_to(admin_people_path) }
      format.xml  { head :ok }
    end
  end
  
  def search
    begin
      @person = Person.new(params[:person])
    rescue
      @person = Person.new
    end
    begin
      @city = City.new(params[:city])
    rescue
      @city = City.new
    end
    #generowanie warunkow
    begin
      #osoby
      cond_tab = Array.new
      query = "1=1"
      params[:person].each do |key, value|
        unless value == ""
          query += " and "+key+" like ?"
          cond_tab.push(  value+"%")
        end
      end
      #miasto
      begin
        cond_tab.push params[:city][:name]+"%"
        query += " and "+" cities.name like ?"
      rescue      
      end 
      @result = Person.paginate :page => params[:page], :per_page => 15, :joins => :city, :order => 'last_name, first_name', :conditions => [query].concat(cond_tab)
    rescue
      @result = Array.new
    end
    
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @person }
    end
  end
end
